package com.yhg.context.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhg.common.utils.R;
import com.yhg.context.entity.BlBlog;
import com.yhg.context.entity.vo.BlogQueryVo;
import com.yhg.context.service.BlBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 凌月青缃
 * @date 2021/1/24
 */
@CrossOrigin
@RestController
@RequestMapping("/context/frontblog")
public class FrontBlogController {

    @Autowired
    BlBlogService blogService;

    //    currentPage: 1,
//    currentSize: 10,
//    sortColumn: 'gmt_create',
//    sortMethod: 'desc'
    @PostMapping("getByPage")
    public R getByPage(@RequestBody BlogQueryVo vo){
        Page<BlBlog> blogPage = new Page<>(vo.getCurrentPage(), vo.getCurrentSize());
        QueryWrapper<BlBlog> wrapper = new QueryWrapper<>();
        if (vo.getSortMethod().equals("desc")){
            wrapper.orderByDesc(vo.getSortColumn());
        }else wrapper.orderByAsc(vo.getSortColumn());
        blogService.page(blogPage,wrapper);

        List<BlBlog> records = blogPage.getRecords();
        long total = blogPage.getTotal();
        return R.ok().data("total",total).data("records",records);
    }
}
